import { Controller, Get, Post, Body, Patch, Param, Delete, Inject, Res, ValidationPipe, UseGuards, HttpException, HttpStatus, ParseIntPipe, Query, DefaultValuePipe, Req, BadRequestException, Session } from '@nestjs/common';
import { UserService } from './user.service';
import { LoginDto } from './dto/login.dto';
import { RegisterDto } from './dto/register.dto';
import { JwtService } from '@nestjs/jwt';
import { Response } from 'express';
import { JwtAuthGuard } from 'src/jwt.auth.guard';
import { Public } from 'src/utils/public';
import { updataUserPassword } from './dto/updataUserPassword';
import { IsAuthority } from 'src/utils/isAdmin';
import { UpdateUser } from './dto/update.dot';
import { isInt } from 'src/utils/isInt';
import * as svgCaptcha from 'svg-captcha';
import session from 'express-session';
@Controller('user')
export class UserController {
  constructor(private readonly userService: UserService) {}
  @Inject(JwtService)
  private jwtService:JwtService

  @Get('code')
  @Public()
  createCaptcha(@Session() session, @Req() req, @Res({passthrough:true}) res) {
    const captcha = svgCaptcha.create({
      size: 4,//生成几个验证码
      fontSize: 50, //文字大小
      width: 100,  //宽度
      height: 34,  //高度
      background: '#cc9966',  //背景颜色
    })
    session.code = captcha.text //存储验证码记录到session
    console.log('----',session.code)
    // res.type('image/svg+xml')
    // return'333'
    return {
      text:captcha.text,
      img:captcha.data
    }
   
  }


  @Post('login')
  @Public()
  // 用户登录
  async login( @Body(ValidationPipe) user:LoginDto,@Res({passthrough:true}) res:Response) {

    
    
    const userObj = await this.userService.login(user)
    console.log('用户登录',userObj)
    if(userObj){
      const token = await this.jwtService.signAsync({
        user:{
          id:userObj.id,
          username:userObj.username,
          role:userObj.role
        }
      })
      return {
        ...userObj,
        token:token
      }
    }
  }

  // @Get('aaa')
  // @Public()

//  aaa(@Session() session){
//     console.log('session----')
//     console.log('session',session.code)
//     console.log('session',session.code.toLocaleLowerCase())
//  }

  //用户注册
  @Post('register')
  @Public()
  async register(@Session() session,@Req()req,@Body(ValidationPipe) user:RegisterDto) {
    if (session.code.toLocaleLowerCase() !== user.code.toLocaleLowerCase()) {
     throw new HttpException('验证码错误',200)
    } 
    return await this.userService.register(user)
  }

  //获取用户列表
  @Get('list')
  @UseGuards(JwtAuthGuard)
  async getUserList(
    @Query('pageNo',new DefaultValuePipe(1), isInt('pageNo')) pageNo: number,
    @Query('pageSize', new DefaultValuePipe(10),isInt('pageSize')) pageSize: number,
    @Query('username') username: string,
    @Query('isDisable') is_disable: string
   ) {
    // return new HttpException('请求失败',HttpStatus.FORBIDDEN)
    return await this.userService.getUserList(pageNo,pageSize,username,is_disable);
  }

  //获取用户详情
  @Get('info/:id')
  @Public()
  async getUserInfo(@Param('id') id: number) {
    return await this.userService.findUserDetailById(id);
  }

  //更新用户密码
  @Post('updatePassword')
  @Public()
  @UseGuards(IsAuthority)
  async updatePassword(@Body(ValidationPipe) updataUserPasswordDto: updataUserPassword) {
      console.log(updataUserPasswordDto);
      return await this.userService.updatePassword(updataUserPasswordDto)
  }
 
  @Post('update')
  @UseGuards(IsAuthority)
  updateUser(@Body(ValidationPipe) updateUserDto: UpdateUser) {
    return this.userService.update(updateUserDto);
  }

  
  @Delete('delete/:id')
  @UseGuards(IsAuthority)
  deleteUser(@Param('id') id: number) {
    return this.userService.delete(id);
  }
}
